//加载动画
function search_Loading_animation() {
    //当页面加载的时候开始执行
    document.getElementById("top").style.display = "none";
    if(document.readyState == "complete") {
        var load = document.getElementById("tou");
        var x = load.style.display = "none";
        document.getElementById("top").style.display = "block";
        console.log(x);
    }
}
//商品搜索，把搜索的商品展示出来
function search_productname() {
    var search_count = 0;
    var product_name = document.getElementById("search_txt").value;
    var url = "merchantByName?product_name=" + product_name;
    $(function () {
        $.ajax({
            url: url,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res.length==0){
                    return;
                }
                var box_table = $("#box_table");
                box_table.empty();
                for (var j = 1; j <= Math.ceil(res.length / 4) ; j++) {
                    var tr = document.createElement("tr");
                    for (var i = search_count; i < j * 4; i++) {
                        var td_1 = document.createElement("td");
                        td_1.setAttribute("align","center");
                        td_1.setAttribute("onmouseover","picturecolor(id)");
                        td_1.setAttribute("onmouseout","picturedefaultcolor(id)")
                        td_1.classList.add("picture");
                        td_1.id = "picture" + i;

                        var a = document.createElement("a");
                        a.setAttribute("href","/MyXiaomiShop/goodsDetail?consignee_id=" + (i+1));
                        a.setAttribute("name",res[i].name);

                        var img_1 = document.createElement("img");
                        img_1.setAttribute("src", res[i].picture);

                        var br_1 = document.createElement("br");

                        var a_1 = document.createElement("a");
                        a_1.setAttribute("href","/MyXiaomiShop/goodsDetail?consignee_id=" + (i+1));
                        a_1.setAttribute("name",res[i].name);
                        a_1.innerText = res[i].name;

                        var br_2 = document.createElement("br");

                        var span = document.createElement("span");
                        span.innerText = res[i].price + "元";
                        span.style.color = "red";

                        a.appendChild(img_1);
                        td_1.appendChild(a);
                        td_1.appendChild(br_1);
                        td_1.appendChild(a_1);
                        td_1.appendChild(br_2);
                        td_1.appendChild(span);
                        tr.appendChild(td_1);
                        box_table.append(tr);
                    }
                    search_count = i;
                }
            },
            error: function (res) {
                alert("所有商品图片调用失败！")
            }
        });
    })
}

//鼠标移入事件，改变购物车的样式
function Shopping_cart_changes1() {
    var top2 = document.getElementById("top2");
    top2.style.backgroundColor="#fffefe";
    var top2_img = document.getElementById("top2_img");
    top2_img.setAttribute("src","img/home/shopping2.png");
    var  a = document.getElementById("top2_a");
    a.style.color = "orange";
}

function Shopping_cart_changes2() {
    var top2 = document.getElementById("top2");
    top2.style.backgroundColor="#424242";
    var top2_img = document.getElementById("top2_img");
    top2_img.setAttribute("src","img/home/shopping1.png");
    var  a = document.getElementById("top2_a");
    a.style.color = "#b0b0b0";
}

//菜单栏a标签的鼠标悬浮和离开事件，主要是实现a标签的交互效果
function topbar_text1(id) {
    var name = document.getElementById(id);
    name.style.color = "#b0b0b0";
}
function topbar_text2(id) {
    var name = document.getElementById(id);
    name.style.color = "white";
}

//标题a标签的鼠标悬浮和离开事件，主要是实现a标签的交互效果
function header_text1(id) {
    var name = document.getElementById(id);
    name.style.color = "black";
}

function header_text2(id) {
    var name = document.getElementById(id);
    name.style.color = "red";
}

//搜索按钮的鼠标悬浮和离开事件
function search_button1(id) {
    var search_id = document.getElementById(id);
    var search_img = document.getElementById("search_img");
    search_img.setAttribute("src","img/home/search1.png");
    search_id.style.backgroundColor = "#ffffff";
}

function search_button2(id) {
    var search_id = document.getElementById(id);
    var search_img = document.getElementById("search_img");
    search_img.setAttribute("src","img/home/search2.png");
    search_id.style.backgroundColor = "#ff6700";
}

//判断是否登录的函数
function login() {
    var user = document.getElementById("top1_login").innerText;
    var top1 = document.getElementById("top1");
    var container_top = document.getElementById("container_top");
    if (user.trim().length == 0){
        document.getElementById("top1_login").innerText = "登录";
        document.getElementById("top2_gwc").innerText = "0";
        top1.style.width = "140px";
        container_top.style.width = "240px";
        container_top.style.marginLeft = "288px";
    }else if (user.trim().length != 0 && user != "登录"){
        document.getElementById("top1_login").setAttribute("href","#");
        top1.style.width = "195px";
        container_top.style.width = "300px";
        container_top.style.marginLeft = "234px";
    }
}

//购物车的点击事件
function shopping_click() {
    var register =  document.getElementById("top2_a").getAttribute("name");
    // alert(register);
    if (register.trim().length==0){
        window.location.href = "/MyXiaomiShop/login.jsp";
    }else {
        window.location.href = "/MyXiaomiShop/cart.jsp";
    }
}

//我的订单点击事件
function my_order() {
    var top1_login = document.getElementById("top1_login").innerText;
    if (top1_login == "登录"){
        window.location.href = "/MyXiaomiShop/login.jsp";
    }else {
        window.location.href = "/MyXiaomiShop/orderList.jsp";
    }
}